/*
 * MIT License
 *
 * Copyright (c) 2025-2048 冰羽
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package cn.star.example;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import java.io.File;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import org.junit.Test;

/**
 * License<br>
 *
 * @author zhaoweiping
 *     <p style='color: red'>Created on 2024-09-23 11:06:00
 * @since 3.0.0
 */
public class License {
  public static final String MIT =
      "/*\n"
          + " * MIT License\n"
          + " *\n"
          + " * Copyright (c) 2024-2048 冰羽\n"
          + " *\n"
          + " * Permission is hereby granted, free of charge, to any person obtaining a copy\n"
          + " * of this software and associated documentation files (the \"Software\"), to deal\n"
          + " * in the Software without restriction, including without limitation the rights\n"
          + " * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n"
          + " * copies of the Software, and to permit persons to whom the Software is\n"
          + " * furnished to do so, subject to the following conditions:\n"
          + " *\n"
          + " * The above copyright notice and this permission notice shall be included in all\n"
          + " * copies or substantial portions of the Software.\n"
          + " *\n"
          + " * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n"
          + " * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n"
          + " * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n"
          + " * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n"
          + " * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n"
          + " * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n"
          + " * SOFTWARE.\n"
          + " */\n\n";

  @Test
  public void execute() {
    String source = "E:\\workspace\\arduino";
    String target = "E:\\tmp\\arduino";

    this.execute(source, target, MIT);
  }

  @Test
  public void clear() {
    String source = "E:\\tmp\\arduino";
    String target = "E:\\tmp\\arduino3";

    this.clear(source, target, MIT);
  }

  /**
   * 扫描处理源码目录文件，将License注释删除，将处理后的文件输出到目标目录
   *
   * @param source 源码目录
   * @param target 目标目录
   * @param license license
   */
  public void clear(String source, String target, String license) {
    String[] x = license.split("\n");
    int length = x.length;
    Function<File, String> handle =
        file -> {
          StringBuilder sb = new StringBuilder();
          List<String> lines = FileUtil.readUtf8Lines(file);
          for (int i = 0; i < lines.size(); i++) {
            if (lines.size() >= length
                && i < length
                && lines.get(0).indexOf("/*") == 0
                && " */".equals(x[length - 1])) {
              continue;
            }
            String line = lines.get(i);
            sb.append(line).append("\n");
          }

          return sb.toString();
        };
    Predicate<File> filter =
        file -> {
          String ext = FileUtil.getSuffix(file);
          return ".java".equalsIgnoreCase(ext) || "java".equalsIgnoreCase(ext);
        };
    this.execute(source, target, handle, filter);
  }

  /**
   * 扫描处理源码目录文件，将License注释添加到文件第一行，将处理后的文件输出到目标目录
   *
   * @param source 源码目录
   * @param target 目标目录
   * @param license license
   */
  public void execute(String source, String target, String license) {
    Function<File, String> handle =
        file -> {
          StringBuilder sb = new StringBuilder();
          List<String> lines = FileUtil.readUtf8Lines(file);
          for (int i = 0; i < lines.size(); i++) {
            String line = lines.get(i);
            if (i == 0) {
              if (StrUtil.isBlank(line) || line.contains("package")) {
                sb.append(license);
              }
            }
            sb.append(line).append("\n");
          }

          return sb.toString();
        };
    Predicate<File> filter =
        file -> {
          String ext = FileUtil.getSuffix(file);
          return ".java".equalsIgnoreCase(ext) || "java".equalsIgnoreCase(ext);
        };
    this.execute(source, target, handle, filter);
  }

  /**
   * 扫描处理源码目录文件，将处理后的文件输出到目标目录
   *
   * @param source 源码目录
   * @param target 目标目录
   * @param handle 处理函数
   * @param filter 过滤出需要处理的文件
   */
  public void execute(
      String source, String target, Function<File, String> handle, Predicate<File> filter) {
    List<File> files = FileUtil.loopFiles(source);
    for (File file : files) {
      String ap = file.getAbsolutePath();
      String tp = target + ap.substring(source.length());
      if (filter.test(file)) {
        String content = handle.apply(file);
        FileUtil.writeUtf8String(content, tp);
      } else {
        FileUtil.copy(file, new File(tp), true);
      }
    }
  }
}
